package com.paomi.client.activity.person;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.person.Adapter.IntegralRecordAdapter;
import com.paomi.client.activity.person.View.IntegralRecordView;
import com.paomi.client.activity.person.model.IntegralDetailBean;
import com.paomi.client.activity.person.model.IntegralListBean;
import com.paomi.client.activity.person.presenter.IntegralRecordPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.refresh.AdapterWrapper;
import com.paomi.client.refresh.SwipeToLoadHelper;
import com.paomi.client.utils.Datas;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IntegralRecordActivity extends BaseMvpActivity<IntegralRecordView, IntegralRecordPre> implements IntegralRecordView,SwipeToLoadHelper.LoadMoreListener  {
    @BindView(R.id.button_goback)
    TextView button_goback;
    @BindView(R.id.tv_obvtain)
    TextView tv_obvtain;
    @BindView(R.id.tv_use)
    TextView tv_use;
    @BindView(R.id.tv_choose_data)
    TextView tv_choose_data;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    IntegralRecordAdapter adapter;
    List<IntegralDetailBean> list = new ArrayList<>();
    String year, month;

    @BindView(R.id.tv_month)
    TextView tv_month;
    @BindView(R.id.ll_none)
    LinearLayout llNone;
    private int pageNum=1;
    //添加上拉加载
    private AdapterWrapper mAdapterWrapper;
    private SwipeToLoadHelper mLoadMoreHelper;

    @NonNull
    @Override
    public IntegralRecordPre createPresenter() {
        return new IntegralRecordPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.my_points);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_integral_record;
    }

    @Override
    protected void initEvent() {
        year = Datas.getDateYear();
        month = Datas.getTimesYue();
        button_goback.setTypeface(iconfont);
        tv_choose_data.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));

        adapter = new IntegralRecordAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.setAdapter(adapter);
        mAdapterWrapper = new AdapterWrapper(adapter);
        mLoadMoreHelper = new SwipeToLoadHelper(recyclerView, mAdapterWrapper);
        mLoadMoreHelper.setLoadMoreListener(this);
        recyclerView.setAdapter(mAdapterWrapper);
        getPresenter().getIntegralList(year, month,pageNum);
        tv_month.setText(year + "-" + month);
    }

    @OnClick(R.id.tv_choose_data)
    void choose() {
        getPresenter().selectDate(this);
    }


    @Override
    public void showIntegralList(IntegralListBean bean) {
        if (bean.returnCode.equals("0000")) {
            if(pageNum==1){
                list.clear();
            }
            if(list.size()>0){
                boolean isData=false;
                List<IntegralDetailBean> listData=bean.getReturnData().getData();
                //判断是否有重复数据
                for(IntegralDetailBean cpllectionInfo :listData){
                    for(int i=0;i<list.size();i++){
                        if(cpllectionInfo.getId() == list.get(i).getId()){
                            isData=false;
                            break;
                        }else{
                            isData=true;
                        }

                    }
                }
                if(isData){
                    list.addAll(bean.getReturnData().getData());
                }else{
                    ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.data_null));
                }
            }else{
                list.addAll(bean.getReturnData().getData());
            }
            adapter.setData(list);
            if (list.size() > 0) {
                llNone.setVisibility(View.GONE);
            } else {
                llNone.setVisibility(View.VISIBLE);
                tv_obvtain.setText("+"+0 + "");
                tv_use.setText("-"+0 + "");
            }
            List<IntegralListBean.Total> list = bean.getReturnData().getTotal();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getOpt() == 0) {
                    tv_obvtain.setText("+"+list.get(i).getValue() + "");
                    tv_use.setText("-"+list.get(i).getOpt() + "");
                } else {
                    tv_use.setText(list.get(i).getOpt() + "");
                }
            }


        } else {
            Util.toast(this, bean.returnMessage);
        }
        mLoadMoreHelper.setLoadMoreFinish();
        mAdapterWrapper.notifyDataSetChanged();
    }

    @Override
    public void showData(String year, String M) {
        pageNum=1;
        month=M;
        getPresenter().getIntegralList(year, M,pageNum);
        if (M.equals(month)) {
            tv_month.setText(year + "-" + M);
        } else {
            tv_month.setText(year + "-" + M);
        }
    }

    @Override
    public void onLoad() {
        pageNum++;
        getPresenter().getIntegralList(year, month,pageNum);
    }
}
